#include <iostream>
#include <cmath>
using namespace std;
#define MAXN 31234
#define N 30000
int fa[MAXN], num[MAXN], pre[MAXN];//pre表示该飞船前面的飞船个数
void init(){
    for(int i=1; i<=N; i++){
        fa[i] = i;
        num[i] = 1;
//        pre[i] = 0;省略
    }
}
int find(int x){
    if(fa[x] == x) return x;
    int grand = find(fa[x]);
    pre[x] += pre[fa[x]];
    return fa[x] = grand;

}

void uni(int a, int b){
    int ga = find(a), gb = find(b);
    fa[ga] = gb;
    pre[ga] = num[gb];
    num[gb] += num[ga];
}

int main(){
    init();
    int T;
    cin >> T;
    while(T--){
        char c;
        int a, b;
        cin >> c >> a >> b;
        if(c == 'M'){
            uni(a, b);
        }
        else{
            if(find(a) == find(b)) cout << abs(pre[a]-pre[b])-1 << endl;
            else cout << "-1" << endl;
        }
    }
}
/*
4
M 2 3
C 1 2
M 2 4
C 4 2

 */
/*模拟，超时，60分
#include <iostream>
#include <list>
using namespace std;
#define MAXN 30000
list<int>lis[MAXN+5];
int fa[MAXN+5]; int pos[MAXN+5];
int find(int x){
    return x==fa[x] ? x : fa[x] = find(fa[x]);
}

int main() {
    for(int i=1; i<=MAXN; i++) fa[i] = i;
    for(int i=1; i<=MAXN; i++) lis[i].push_back(i);
    fill(pos+1, pos+1+MAXN, 1);
    int T;
    cin >> T;
    for(int i=1; i<=T; i++){
        char m; int a, b;
        cin >> m >> a >> b;
        if(m == 'M'){
            int aa = find(a), bb = find(b);
            fa[aa] = bb;

            int bsize = lis[bb].size();
            list<int>::iterator it;
            for(it = lis[aa].begin(); it!=lis[aa].end(); ++it){
                pos[*it] += bsize;
            }
            lis[bb].splice(lis[bb].end(), lis[aa]);
        }
        else{
            int aa = find(a), bb = find(b);
            if(aa != bb) cout << "-1" << endl;
            else{
                cout << abs(pos[a] - pos[b])-1 << endl;
            }
        }
    }
    return 0;
}
 */
